﻿
Partial Class WebForm_ImportVolunteer
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.TabContainer1.ActiveTab = Me.SearchVolunteerTab
            Me.BindData()
        End If
    End Sub

    Protected Sub ImportVolunteer_Search_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ImportVolunteer_Search1.SelectedIndexChanged
        '單筆檢視模式
        Me.TabContainer1.ActiveTab = Me.AddTab
        Me.ImportVolunteer_Add1.InitData(Me.ImportVolunteer_Search1.SelectedImportID)

    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
        'MyBase.VerifyRenderingInServerForm(control)
    End Sub

 
    'Protected Sub TabContainer1_ActiveTabChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '    If (Me.TabContainer1.ActiveTabIndex = 0) Then
    '        Me.ImportVolunteer_Search1.BindGV()
    '    End If
    'End Sub
    'Protected Sub SearchVolunteer1_Loaded(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchVolunteer1.Loaded
    '    Me.TabContainer1.ActiveTab = Me.SearchVolunteerTab
    'End Sub

    Protected Sub SearchVolunteer1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchVolunteer1.SelectedIndexChanged
        Me.TabContainer1.ActiveTab = Me.AddVolunteerTab
        Me.AddVolunteerTab.Visible = True
        Me.AddVolunteer1.GetVOrganizationID = Me.SearchVolunteer1.VOrganizationID
        Me.AddVolunteer1.InitData(Me.SearchVolunteer1.SelectedValue)
    End Sub

    Protected Sub ImportVolunteer_Add1_Loaded(ByVal source As Object, ByVal e As System.EventArgs) Handles ImportVolunteer_Add1.Loaded
        Me.TabContainer1.ActiveTab = Me.AddTab
    End Sub

    Protected Sub ImportVolunteer_Add1_Saved(ByVal source As Object, ByVal e As System.EventArgs) Handles ImportVolunteer_Add1.Saved
        Me.TabContainer1.ActiveTab = Me.SearchTab
        Me.ImportVolunteer_Search1.BindGV()
    End Sub

    Protected Sub AddVolunteer1_ChangeStatus(ByVal source As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.ChangeStatus
        Me.TabContainer1.ActiveTab = Me.SearchVolunteerTab
        Me.SearchVolunteer1.BindGV()
    End Sub


    Protected Sub AddVolunteer1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.Saving
        If Me.SaveData = True Then
            If Me.AddVolunteer1.IsChangeStatus = False Then
                Me.TabContainer1.ActiveTab = Me.SearchVolunteerTab
                Me.SearchVolunteer1.BindGV()
            End If
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
        End If
    End Sub
End Class

Partial Class WebForm_ImportVolunteer
    Private Sub BindData()
        Me.ImportVolunteer_Add1.InitData()
    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim i As Integer
        'If Me.AddVolunteer1.VerificationCode <> Me.AddVolunteer1.VerificationCode Then
        '    MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_InvalidVerificationCode"))
        '    Return False
        'End If
        '檢查局列管及備註長度
        If Me.AddVolunteer1.BlackList.Length > 200 Then
            MyObj.ShowMessage("局列管名單不可超過兩百字!")
            Return False
        End If

        If Me.AddVolunteer1.Comment.Length > 600 Then
            MyObj.ShowMessage("備註不可超過六百字!")
            Return False
        End If

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("update Volunteer set ")
            sql.AppendFormat(" VolunteerName=N'{0}' ", Me.AddVolunteer1.VolunteerName)
            sql.AppendFormat(" ,EcolifeID='{0}' ", Me.AddVolunteer1.EcolifeID)

            'sql.AppendFormat(",IdentificationID='{0}' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",IdentificationType='{0}' ", Me.AddVolunteer1.IdentificationType)
            sql.AppendFormat(",UpdateDate={0} ", "getdate()")
            sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
            sql.AppendFormat(",Gender='{0}' ", Me.AddVolunteer1.Gender)
            sql.AppendFormat(",Birthday='{0:yyyy/MM/dd}' ", Me.AddVolunteer1.Birthday)
            sql.AppendFormat(",MobilePhone='{0}' ", Me.AddVolunteer1.MobilePhone)
            sql.AppendFormat(",OfficePhone='{0}' ", Me.AddVolunteer1.OfficePhone)
            sql.AppendFormat(",BirthVillageID='{0}' ", Me.AddVolunteer1.BirthRegionID)
            sql.AppendFormat(",BirthAddress='{0}' ", Me.AddVolunteer1.BirthAddress)
            sql.AppendFormat(",SameAddress='{0}' ", Me.AddVolunteer1.SameAddress)
            sql.AppendFormat(",VillageID='{0}' ", Me.AddVolunteer1.RegionID)
            sql.AppendFormat(",Address='{0}' ", Me.AddVolunteer1.Address)
            sql.AppendFormat(",EmergentPerson='{0}' ", Me.AddVolunteer1.EmergentPerson)
            sql.AppendFormat(",EmergentPhone='{0}' ", Me.AddVolunteer1.EmergentPhone)
            sql.AppendFormat(",Email='{0}' ", Me.AddVolunteer1.Email)
            sql.AppendFormat(",EducationLevel='{0}' ", Me.AddVolunteer1.EducationLevel)
            sql.AppendFormat(",OccupationType='{0}' ", Me.AddVolunteer1.OccupationType)
            sql.AppendFormat(",Retired='{0}' ", Me.AddVolunteer1.Retired)
            sql.AppendFormat(",BlackList='{0}' ", Me.AddVolunteer1.BlackList)
            sql.AppendFormat(",PersonName='{0}' ", Me.AddVolunteer1.PersonName)
            sql.AppendFormat(",Comment ='{0}' ", Me.AddVolunteer1.Comment)
            sql.AppendFormat(",Status ='{0}' ", Me.AddVolunteer1.Status)
            sql.AppendFormat(",RecoverDate='{0:yyyy/MM/dd}' ", Me.AddVolunteer1.RecoverDate)
            sql.AppendFormat("where VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '希望隸屬單位
            Dim CheckCount As Integer = 0
            sql.Length = 0
            sql.AppendFormat("delete from WishOrganization where ")
            sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
            For i = 0 To Me.AddVolunteer1.WishOrganization.Length - 1
                If (Me.AddVolunteer1.WishOrganization(i) <> "") Then
                    sql.AppendFormat("insert WishOrganization (VolunteerID,OrganizationID) select ")
                    sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                    sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WishOrganization(i).ToString())
                    CheckCount += 1
                End If
            Next
            If CheckCount > MyObj.GetParameter("Register", "WishOrganizationCount") Then
                MyObj.ShowMessage(Me.GetLocalResourceObject("OverWishOrganization"))
                Return False
            End If
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '可服務時間
            sql.Length = 0
            sql.AppendFormat("delete from VolunteerWorkTime where ")
            sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
            For i = 0 To 2
                sql.AppendFormat("insert VolunteerWorkTime select ")
                sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                Select Case i
                    Case 0
                        sql.AppendFormat(",'{0}' ", "A")
                    Case 1
                        sql.AppendFormat(",'{0}' ", "N")
                    Case 2
                        sql.AppendFormat(",'{0}' ", "P")
                    Case Else
                        sql.AppendFormat(",'{0}' ", "A")
                End Select
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 0))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 1))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 2))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 3))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 4))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 5))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 6))
            Next
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '訓練時數、已服務時數累計(未審核通過前才可更新)

            '訓練時數
            'sql.Length = 0
            'sql.AppendFormat("delete from OtherTrainingRecord where ")
            'sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
            'For i = 0 To 2
            '    sql.AppendFormat("insert OtherTrainingRecord (VolunteerID,TrainingHour,TrainingType,CreateBy,CreateDate,Active) select ")
            '    sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
            '    sql.AppendFormat(" ,'{0}' ", Me.AddVolunteer1.TrainingHour(i))
            '    Select Case i
            '        Case 0
            '            sql.AppendFormat(",'{0}' ", "B")
            '        Case 1
            '            sql.AppendFormat(",'{0}' ", "S")
            '        Case 2
            '            sql.AppendFormat(",'{0}' ", "W")
            '        Case Else
            '            sql.AppendFormat(",'{0}' ", "B")
            '    End Select
            '    sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
            '    sql.AppendFormat(",{0} ", "getdate()")
            '    sql.AppendFormat(",'Y'")
            'Next
            'dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '已服務時數累計
            'sql.Length = 0
            'sql.AppendFormat("delete from OtherWorkRecord where ")
            'sql.AppendFormat(" VolunteerID='{0}' ", Me.AddVolunteer1.VolunteerID)
            'sql.AppendFormat("insert OtherWorkRecord (VolunteerID,WorkHour,WorkTimes,CreateDate,CreateBy,Active) select ")
            'sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
            'sql.AppendFormat(",{0} ", Me.AddVolunteer1.OtherWorkHour)
            'sql.AppendFormat(",1")
            'sql.AppendFormat(",{0} ", "getdate()")
            'sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
            'sql.AppendFormat(",'N'")
            'dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            scope.Complete()
        End Using

        Return True
    End Function

    Private ReadOnly Property LoginID() As String
        Get
            Return Context.User.Identity.Name
        End Get
    End Property

End Class